<%@ page contentType="text/html;charset=UTF-8" language="java"  %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon" />
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<script src="${pageContext.request.contextPath}/layui/layui.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath}/js/jquery-3.3.1.min.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>
    layui.use('table', function(){

        var table = layui.table;
        var pn ;//当前页
        var pc ;//当前页分几条数据


        table.render({
            elem: '#test'
            ,url:'${pageContext.request.contextPath}/FindAllBookServlet.action'
            ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '图书表数据'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'bid', title:'编号',  fixed: 'left', unresize: true, sort: true,align:'center'}
                ,{field:'bname', title:'书名', edit: 'text'}
                ,{field:'bauthor', title:'作者',  edit: 'text'}
                ,{field:'bprice', title:'价格',  edit: 'text', sort: true}
                ,{field:'bdate', title:'出版日期'}
                ,{field:'sid', title:'出版社编号',sort:true}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]]
            ,page: true,
            limit:10,
            request: {
                pageName: 'pageNumber' //页码的参数名称，默认：page
                ,limitName: 'pageCount' //每页数据量的参数名，默认：limit
            }
            ,response: {
            statusName: 'code'
                ,msgName:'msg'
                ,countName: 'total' //数据总数的字段名称，默认：count
                ,dataName: 'data' //数据列表的字段名称，默认：data
            },done:function(res, curr, count){
//                    alert(res);//响应数据
//                    alert(curr);//当前页
//                    alert(count);//总记录数
                       pc = res.pageCount; //得到当前页分几条数据
                       pn = curr ;//得到当前页
            }

        });
        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选');
                    break;

                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });





        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    //obj.del();//删除此条数数
                    $.post("DeleteBookByIdServlet.action","bid="+data.bid,function(data){
                        /*if(data.flag){
                            alert("成功");
                        }else{
                            alert("失败");
                        }*/
                        //window.location.replace();//刷新
                        console.log(pn);
                        console.log(pc);
                        table.reload('test',{//重载表格
                            page:{
                                curr:pn,
                                count:pc
                            }
                        })
                    },"json");
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){

                //------------------------------------------------
                layer.open({
                     type: 1 //Page层类型
                    ,skin: 'layui-layer-molv'
                    ,area: ['500px', '500px']
                    ,title: ['编辑书本类别信息','font-size:18px']
                    ,btn: ['确定', '取消']
                    ,shadeClose: true
                    ,shade: 0 //遮罩透明度
                    ,maxmin: true //允许全屏最小化
                    ,content:$('#box1')  //弹窗id
                    ,success:function(layero,index){
                        $('#bid').val(data.bid);
                        $('#bname').val(data.bname);
                        $('#bauthor').val(data.bauthor);
                        $('#bprice').val(data.bprice);
                        $('#bdate').val(data.bdate);

                        $.get("${pageContext.request.contextPath}/ShowSupplierServlet.action?xx="+Math.random(),"",function(data){
                            for(var i=0 ; i < data.length ;i++){
                                $("#sid").append("<option value='"+data[i].sid+"'>"+data[i].sname+"</option>");
                            }
                            form.render('select');//如果这段代码无法显示下拉菜中的数据
                        },"json");

                        $("#sid").val(data.sid);
                    },yes:function(index,layero){
                        /*$.ajaxSettings.async = false;
                        $.getJSON(urlex+'/booktypeAction.action?methodName=updateBookType',{
                            booktypeid: $('#booktypeid').val(),
                            booktypename: $('#booktypename1').val(),
                            booktypeid: data.booktypeid,
                        },function(data){
                            //根据后台返回的参数，来进行判断
                            if(data>0){
                                layer.alert('编辑成功',{icon:1,title:'提示'},function(i){
                                    layer.close(i);
                                    layer.close(index);//关闭弹出层
                                    $("#booktype")[0].reset()//重置form
                                })
                                table.reload('testReload',{//重载表格
                                    page:{
                                        curr:1
                                    }
                                })
                            }
                        });*/



                        $.post("${pageContext.request.contextPath}/UpdateBookServlet.action",$("#box1").serialize(),function(data){

                            console.log(data.flag);
                            //根据后台返回的参数，来进行判断
                            if(data.flag){
                                layer.alert('编辑成功',{icon:1,title:'提示'},function(i){
                                    layer.close(i);
                                    layer.close(index);//关闭弹出层
                                    $("#box1")[0].reset()//重置form
                                })
                                table.reload('test',{//重载表格
                                    page:{
                                        curr:pn,
                                        count:pc
                                    }
                                })
                            }

                        },"json");

                    }
                });
                //--------------------------------------------------------


            }
        });





    });
</script>


<form class="layui-form" action="${pageContext.request.contextPath}/UpdateBookServlet.action" id="box1" style="display: none;">
    <input name="bid" id="bid" type="hidden">
    <div class="layui-form-item">
        <label class="layui-form-label">书名:</label>
        <div class="layui-input-block">
            <input type="text" name="bname" id="bname" lay-verify="title" autocomplete="off" placeholder="请输入图书" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">作者:</label>
        <div class="layui-input-block">
            <input type="text" name="bauthor" id="bauthor" lay-verify="title" autocomplete="off" placeholder="请输入作者" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">价格:</label>
        <div class="layui-input-block">
            <input type="text" name="bprice" id="bprice" lay-verify="title" autocomplete="off" placeholder="请输入价格" class="layui-input">
        </div>
    </div>

    <div class="layui-inline">
        <label class="layui-form-label">出版日期</label>
        <div class="layui-input-inline">
            <input type="text" name="bdate" id="bdate" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">单行选择框</label>
        <div class="layui-input-block">
            <select name="sid" id="sid">
            </select>
        </div>
    </div>


</form>

</body>
</html>

<script>
    var form;
    var layer;
    layui.use(['form', 'layedit', 'laydate'], function(){
        var laydate = layui.laydate;
        form = layui.form;
        layer = layui.layer

        //日期
        laydate.render({
            elem: '#bdate'
        });
    });
</script>